package com.topboya.linker.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.topboya.linker.R;
import com.topboya.linker.model.response.FeekbackReponse;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class FeedBackAdapter extends RecyclerView.Adapter<FeedBackAdapter.ListItemViewHolder> {

    private static final String TAG = "SelectProductAdapter:";
    private List< FeekbackReponse.FeekbackData> mList;
    private Context mContext;
    //   用来保存被选中的对象
    private LinkedHashMap<String, FeekbackReponse.FeekbackData> mSelectedPositions = new LinkedHashMap<>();


    public FeedBackAdapter(Context mContext, List<FeekbackReponse.FeekbackData> list) {

        this.mContext = mContext;
        mList = list;
    }


    //获得选中条目的结果
    public ArrayList<FeekbackReponse.FeekbackData> getSelectedItem() {

        ArrayList<FeekbackReponse.FeekbackData> selectList = new ArrayList<>();
        for (Map.Entry<String, FeekbackReponse.FeekbackData> entry : mSelectedPositions.entrySet()) {

            FeekbackReponse.FeekbackData productDetailInfoList = entry.getValue();
            selectList.add(productDetailInfoList);

        }
        return selectList;
    }


    @Override
    public ListItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.feek_back_item, viewGroup, false);
        return new ListItemViewHolder(itemView);
    }

    //设置给定位置条目的选择状态
    public void setItemChecked(String id, FeekbackReponse.FeekbackData feekbackDataList) {
        mSelectedPositions.clear();
        mSelectedPositions.put(id, feekbackDataList);
    }

    //设置给定位置条目的选择状态
    public void setItemUnChecked(String id) {

        mSelectedPositions.remove(id);
    }


    //根据位置判断条目是否选中
    private  FeekbackReponse.FeekbackData isItemChecked(int position) {

        return mSelectedPositions.get(mList.get(position).getId());
    }


    //绑定界面，设置监听
    @Override
    public void onBindViewHolder(final ListItemViewHolder holder, final int i) {

        holder.tv_feekback_label.setText(mList.get(i).getLabel());
        if (null != isItemChecked(i)) {

            holder.tv_feekback_label.setBackground(mContext.getResources().getDrawable(R.drawable.select_feedback_oval_bg));
            holder.tv_feekback_label.setTextColor(Color.WHITE);
        } else {

            holder.tv_feekback_label.setBackground(mContext.getResources().getDrawable(R.drawable.no_select_feedback_bg));
            holder.tv_feekback_label.setTextColor(mContext.getResources().getColor(R.color.no_select_feedback_color));
        }
        //条目view的监听
        holder.itemView.setOnClickListener(v -> {

            if (null != isItemChecked(i)) {

                setItemUnChecked(mList.get(i).getId());

            } else {

                setItemChecked(mList.get(i).getId(),mList.get(i));
            }
            notifyDataSetChanged();
            if (null !=itemClickListener){

                itemClickListener.onClick(holder.itemView, i);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public class ListItemViewHolder extends RecyclerView.ViewHolder {

        //ViewHolder
        TextView tv_feekback_label;

        ListItemViewHolder(View view) {
            super(view);
            this.tv_feekback_label = view.findViewById(R.id.tv_feekback_label);

        }

    }

    private OnItemClickListener itemClickListener;

    public void setOnItemClickListener(OnItemClickListener itemClickListener) {

        this.itemClickListener = itemClickListener;
    }

    public interface OnItemClickListener {

        void onClick(View v, int position);
    }

}


